/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frexport.c                                         */
/*  NOTE      =                                                    */
/*  DATE      = 2016/01/07 by guojq                                */
/*******************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "client.pb.h"
#include "frexport_pcap.h"
#include "frcmd_server.h"

/*******************************************************************************
*  FUNC     :  做导出样本包操作
*  ARGS     :  ui_ip          样本包的抓包ip地址
*           :  ui_time        样本包抓包时长
*  RTN      :  FR_SUCC (success)
*           :  FR_FAIL (failed)
*  NOTE     :
*******************************************************************************/
static int __do_export_download(u_int ui_ip, u_int ui_time, const char *p_dump_filename)
{
    char arr_cmd[512];
    int i_status = 0;

    if (p_dump_filename == NULL)
        return FR_FAIL;

    //组成shell命令
    snprintf(arr_cmd, sizeof(arr_cmd), "sh %s %s %d %s &"
           , EXPORT_CMD_NAME, int_ntoa(ui_ip), ui_time, p_dump_filename);

    i_status = system(arr_cmd);
    if (i_status < 0)
        return FR_FAIL;

    return FR_SUCC;
}

/*******************************************************************************
*  FUNC     :  做导出样本包命令主接口
*  ARGS     :  p_command        命令
*           :  pp_result        错误信息
*  RTN      :  FR_SUCC (success)
*           :  FR_FAIL (failed)
*  NOTE     :
*******************************************************************************/
int do_export_cmd(Command *p_command, char **pp_result)
{
    ExportCommand st_export_cmd;

    if (p_command == NULL || !p_command->has_export_cmd())
        goto err;

    st_export_cmd = p_command->export_cmd();

    return __do_export_download(st_export_cmd.ip(), st_export_cmd.time(), st_export_cmd.dump_filename().c_str());
err:
    *pp_result = frstrdup(FRCMD_SERVER_NOT_SUPPORT);
    return FR_FAIL;
}

